#ifndef _TD_SDLIMAGE_H_
#define _TD_SDLIMAGE_H_

/************************************************************************/
/* STL                                                                  */
/************************************************************************/
#include <string>
#include <iostream>
#include <fstream>
#include <exception>

/************************************************************************/
/* SDL                                                                  */
/************************************************************************/
#include "SDL/SDL.h"
#include "SDL_image/SDL_image.h" 

/************************************************************************/
/* Core                                                                 */
/************************************************************************/
#include "Core/Coord2D.h"
#include "Core/Color.h"

/************************************************************************/
/* Resources                                                            */
/************************************************************************/
#include "Resources/Resource.h"

using namespace std;
using namespace TD_Core;


namespace TD_Resources {

	class SDLImage : public Resource {
		
		//Raw SDL image pointer
		SDL_Surface *m_pImage;
		
		//Image bounds
		Coord2D m_bounds;

		/************************************************************************/
		/* Private functions                                                    */
		/************************************************************************/
		bool optimizeImg();
		
	protected:

		inline SDL_Surface * const getSDLSurface() const {
			return m_pImage;
		}

	public:
		
		/************************************************************************/
		/* Constructors				                                            */
		/************************************************************************/
		//Constructor default
		explicit SDLImage();

		//Copy constructor
		explicit SDLImage(const SDLImage &rhs);

		//Assignment constructor
		SDLImage & operator=(const SDLImage &rhs);
		
		//Initialization constructor
		explicit SDLImage(const string &resourceName);

		//Destructor 
		~SDLImage();
		
		/************************************************************************/
		/* Access functions                                                     */
		/************************************************************************/
		const Coord2D & getBounds() const {
			return (m_bounds);
		}

		void setSurface(SDL_Surface *g_pImage) {
			SDL_FreeSurface(m_pImage);
			m_pImage = g_pImage;
		}
		SDL_Surface* getSurface() const {
			return m_pImage;
		}

		/************************************************************************/
		/* Functionalities                                                      */
		/************************************************************************/
		
		/**Loads a SDL_Surface from a file*/
		bool loadFile(const string &file);

		/**Loads a keyed surface from a file*/
		bool loadColorKeyFile(const string &file, const Color &keyColor);
		
		/**Blits into a destination surface (plots), given a world offset*/ 
		void blitSurface(SDL_Surface * const g_pSurface, SDL_Rect *g_worldOffset, SDL_Rect *g_pClippingOffset = NULL);
		
		/************************************************************************/
		/* Rendering                                                            */
		/************************************************************************/
		
		virtual void render(SDL_Surface *g_destSurface, unsigned int deltaTime);
	};
}
#endif